package service

import (
	"api-gateway-center/domain/manage/model"
	"api-gateway-center/domain/manage/repository"
	constants "api-gateway-center/types/common"
)

type ConfigManageService struct {
	repository repository.ConfigManageRepository
}

func NewConfigManageService(repository repository.ConfigManageRepository) *ConfigManageService {
	return &ConfigManageService{repository: repository}
}

// QueryGatewayDistribution
//
//	@Description: 查询网关分配情况
//	@receiver this
//	@param systemId 系统ID
//	@return string 网关ID
//	@return error
func (this *ConfigManageService) QueryGatewayDistribution(systemId string) (string, error) {
	distribution, err := this.repository.QueryGatewayDistribution(systemId)
	if err != nil {
		return "", err
	}
	return distribution, nil
}

// RegisterGatewayServerNode
//
//	@Description: 向网关明细表注册
//	@receiver this
//	@param groupId
//	@param gatewayId
//	@param gatewayName
//	@param gatewayAddress
//	@return error
func (this *ConfigManageService) RegisterGatewayServerNode(groupId, gatewayId, gatewayName, gatewayAddress string) error {
	gatewayServerDetailVO, err := this.repository.QueryGatewayServerDetail(gatewayId, gatewayAddress)
	if err != nil || nil == gatewayServerDetailVO {
		return this.repository.RegisterGatewayServerNode(groupId, gatewayId, gatewayName, gatewayAddress, constants.Available)
	}
	return this.repository.UpdateGatewayStatus(gatewayId, gatewayAddress, constants.Available)
}

// QueryApplicationSystemRichInfo
//
//	@Description: 查询应用整体信息
//	@receiver this
//	@param systemId 系统ID
//	@param gatewayId 网关ID
//	@return *model.ApplicationSystemRichInfo 结果
//	@return error
func (this *ConfigManageService) QueryApplicationSystemRichInfo(systemId, gatewayId string) (*model.ApplicationSystemRichInfo, error) {
	var systemIdList []string
	if "" == systemId {
		systemIds, _ := this.repository.QueryGatewayDistributionSystemIdList(gatewayId)
		systemIdList = append(systemIdList, systemIds...)
	} else {
		systemIdList = append(systemIdList, systemId)
	}
	applicationSystemVOList, err := this.repository.QueryApplicationSystemVOList(systemIdList)
	if err != nil {
		return nil, err
	}
	for _, applicationSystemVO := range applicationSystemVOList {
		interfaceList, err := this.repository.QueryApplicationInterfaceList(applicationSystemVO.SystemId)
		if err != nil {
			return nil, err
		}
		for _, applicationInterfaceVO := range interfaceList {
			applicationInterfaceMethodVOList, err := this.repository.QueryApplicationInterfaceMethodList(applicationSystemVO.SystemId, applicationInterfaceVO.InterfaceId)
			if err != nil {
				return nil, err
			}
			for _, interfaceMethodVO := range applicationInterfaceMethodVOList {
				applicationInterfaceVO.MethodList = append(applicationInterfaceVO.MethodList, interfaceMethodVO)
			}
			applicationSystemVO.InterfaceList = append(applicationSystemVO.InterfaceList, applicationInterfaceVO)
		}
	}
	applicationSystemRichInfo := model.NewApplicationSystemRichInfo(gatewayId, applicationSystemVOList)
	return &applicationSystemRichInfo, err
}
func (this *ConfigManageService) QueryGatewayServerDetailList() ([]*model.GatewayServerDetailVO, error) {
	return this.repository.QueryGatewayServerDetailList()
}
